<?php

class Action extends Controller {

    function Action() {
        parent::Controller();
        $this->load->library('YB_Actions');
        $this->load->library('YB_Board');
    }

    function index() {
        $this->load->view('welcome_message some some new change');
    }

     /**
     * These functions insert data into database.
     */

    //add a thread to forum
    function create_thread() {

        if($this->uri->segment(3)==FALSE)
        {die("404 error. Please use post button to post the data and don't go for direct urls. <br> Thank you,<br>Yash");}

        $val = $this->form_validation;

        // Set form validation rules
        $val->set_rules('thread_title', 'Thread Title', 'required|callback__check_url_title|xss_clean');
        $val->set_rules('thread_content', 'Thread Content', 'required|xss_clean');
        $val->set_rules('thread_desc', 'Thread Description', 'required|xss_clean');

        if (!$val->run()) {
            $data['title']='forum';
            $data['content']=$this->load->view('board/template/post_thread',$data,TRUE);
            $this->load->view('main',$data);
        }
        else {
        //save thread
            $this->yb_actions->create_thread();
        }
    }	//end of function post_thread

     function manage_threads($pageid=1) {
        $data['title']="Thread Management - Forum";
        $data['content']=$this->Threadmodel->list_all_threads($pageid);
        $this->load->view('admin/main',$data);
    }


    function delete_thread($thread_id,$page_id) {
        $this->Threadmodel->delete_thread($thread_id,$page_id);
    }

    function edit_thread($thread_id,$page_id=1) {
        $rules['thread_content'] = "required";
        $rules['thread_title'] = "required";
        $this->validation->set_rules($rules);

        $fields['thread_content'] = 'thread content';
        $fields['uploaded_files'] = 'uploaded';
        $fields['thread_title'] = 'thread title';
        $fields['thread_desc'] = 'thread desc';
        $fields['make_announcement'] = 'announcement';
        $fields['open_date'] = 'open date';
        $fields['close_date'] = 'close date';
        $fields['meta'] = 'meta';
        $fields['enable_emoticons'] = 'emoticons';
        $fields['enable_signature'] = 'signature';
        $fields['enable_notification'] = 'notification';
        $fields['reason_content'] = 'reason';
        $fields['icon_id'] = 'icon id';
        $fields['keyword'] = 'keyword';
        $fields['description'] = 'desc';
        $this->validation->set_fields($fields);

        if (empty($_POST)) {
            $this->db->where(array('thread_id'=>$thread_id));
            $query=$this->db->get('board_threads');
            $post=$query->row_array();

            $this->validation->thread_content = $post['thread_content'];
            $this->validation->thread_title = $post['thread_title'];
            $this->validation->thread_desc = $post ['thread_desc'];
            $this->validation->meta = $post ['meta'];
            $this->validation->keyword = $post ['keyword'];
            $this->validation->description = $post ['description'];
            if($post['enable_emoticons']==1)
                $this->validation->set_checkbox('enable_emoticons', '1');
            else
                $this->validation->set_checkbox('enable_emoticons', '0')	;

            if($post['enable_signature']==1)
                $this->validation->set_checkbox('enable_signature', '1');
            else
                $this->validation->set_checkbox('enable_signature', '0');


            if($post['enable_notification']==1)
                $this->validation->set_checkbox('enable_notification', '1');
            else
                $this->validation->set_checkbox('enable_notification', '0');

            $this->validation->icon_id = $post['icon_id'];


        }

        if ($this->validation->run()) {
            $this->Threadmodel->edit_thread($thread_id,$page_id);
        }
        else {
            $data['title']="Edit Thread - Forum";
            $data['content']=$this->load->view('users/edit_thread',$data,TRUE);
            $this->load->view('admin/main',$data);

        }
    }



    // Check url tile of threads
    function _check_url_title($title) {

        $query = $this->db->getwhere('board_threads', array('thread_title'=>$title), 1, 0);
        if ($query->num_rows() != 0) {
            $this->validation->set_message('_check_url_title', 'A thread is present with this <b>title</b>.');
            return FALSE;
        }
        return TRUE;
    }




    //add reply to a thread
    function create_reply() {

        if($this->uri->segment(3)==FALSE || $this->uri->segment(4)==FALSE)
        {die("404 error. Please use post button to post the data and don't go for direct urls. <br> Thank you,<br>Yash");}


    //adding rules for posts
        $rules['post_content'] = "required|xss_clean|trim";
        $this->form_validation->set_rules($rules);

        if (!$this->form_validation->run()) {

            $data['title']='forum';
            $data['content']=$this->load->view('board/template/post_reply',$data,TRUE);
            $this->load->view('main',$data);

        }
        else {
        //save reply
            $this->yb_actions->create_reply();
        }

    } //end of function
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */